/**
 * 2012-3-24 BWL
 *2012-4-11 添加转换逻辑
 */
//嵌入Flash代码开始
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var is_mac = userAgent.indexOf('mac') != -1;
function AC_GetArgs(args, classid, mimeType){
    var ret = new Object();
    ret.embedAttrs = new Object();
    ret.params = new Object();
    ret.objAttrs = new Object();
    for (var i = 0; i < args.length; i = i + 2) {
        var currArg = args[i].toLowerCase();
        switch (currArg) {
            case "classid":
                break;
            case "pluginspage":
                ret.embedAttrs[args[i]] = 'http://www.macromedia.com/go/getflashplayer';
                break;
            case "src":
                ret.embedAttrs[args[i]] = args[i + 1];
                ret.params["movie"] = args[i + 1];
                break;
            case "codebase":
                ret.objAttrs[args[i]] = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0';
                break;
            case "onafterupdate":
            case "onbeforeupdate":
            case "onblur":
            case "oncellchange":
            case "onclick":
            case "ondblclick":
            case "ondrag":
            case "ondragend":
            case "ondragenter":
            case "ondragleave":
            case "ondragover":
            case "ondrop":
            case "onfinish":
            case "onfocus":
            case "onhelp":
            case "onmousedown":
            case "onmouseup":
            case "onmouseover":
            case "onmousemove":
            case "onmouseout":
            case "onkeypress":
            case "onkeydown":
            case "onkeyup":
            case "onload":
            case "onlosecapture":
            case "onpropertychange":
            case "onreadystatechange":
            case "onrowsdelete":
            case "onrowenter":
            case "onrowexit":
            case "onrowsinserted":
            case "onstart":
            case "onscroll":
            case "onbeforeeditfocus":
            case "onactivate":
            case "onbeforedeactivate":
            case "ondeactivate":
            case "type":
            case "id":
                ret.objAttrs[args[i]] = args[i + 1];
                break;
            case "width":
            case "height":
            case "align":
            case "vspace":
            case "hspace":
            case "class":
            case "title":
            case "accesskey":
            case "name":
            case "tabindex":
                ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i + 1];
                break;
            default:
                ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i + 1];
        }
    }
    ret.objAttrs["classid"] = classid;
    if (mimeType) {
        ret.embedAttrs["type"] = mimeType;
    }
    return ret;
}

function AC_FL_RunContent(){
    var ret = AC_GetArgs(arguments, "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000", "application/x-shockwave-flash");
    var str = '';
    if (is_ie && !is_opera) {
        str += '<object ';
        for (var i in ret.objAttrs) {
            str += i + '="' + ret.objAttrs[i] + '" ';
        }
        str += '>';
        for (var i in ret.params) {
            str += '<param name="' + i + '" value="' + ret.params[i] + '" /> ';
        }
        str += '</object>';
    }
    else {
        str += '<embed ';
        for (var i in ret.embedAttrs) {
            str += i + '="' + ret.embedAttrs[i] + '" ';
        }
        str += '></embed>';
    }
    return str;
}

//嵌入Flash代码结束

function soapWrapFunc(bdStr){
    // 封装SOAP头尾
    var soapHead = '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/"><env:Body>', soapTail = '</env:Body></env:Envelope>';
    return soapHead + bdStr + soapTail;
}

//将性别编号变成文字
function sexToText(sexNum){
    return sexNum == '1' ? '男' : '女';
}

//提取日期
function dateToText(utcTime){
    var dateReg = /\d{4}\-\d{2}\-\d{2}/;
    return $.trim(utcTime.match(dateReg));
}

//政治面貌转换
function plaffToText(index){
    return index == '0' ? '普通群众' : (index == '1' ? '共青团员' : (index == '2' ? '预备党员' : '共产党员'));
}

//考生来源
function sSrcToText(index){
    return index == '0' ? '应届' : '其他';
}

//技术职称转换
function techTilToText(index){
    return index == '0' ? '无' : (index == '1' ? '初级' : (index == '2' ? '中级' : '高级'));
}

//加载学生详细信息
function loadStuDetailData(id){
    //验证报文
    var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
    var str = '<web:GetStudentDetailInfoById>' + userinfo + '<id>' + id + '</id></web:GetStudentDetailInfoById>';
    $.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/StudentService',
        data: soapWrapFunc(str),
        dataType: 'xml',
        success: function(xml){
            var xmlObj = $(xml), stuid = xmlObj.find('stubs_id').text();
            
            if (stuid != '0') {
                $.curStudentId = stuid;
                //将返回的XML节点和TD对应进行赋值
                var setTdText = function(tdId, xmlNode, someFunc){
                    var xmlNodeVal = xmlObj.find(xmlNode).text();
                    if (someFunc == undefined) {
                        $('#' + tdId).text(xmlNodeVal);
                    }
                    else {
                        //数据处理逻辑
                        $('#' + tdId).text(someFunc(xmlNodeVal));
                    }
                }
                setTdText('tdName', 'stubs_name');
                setTdText('tdSex', 'stubs_sex', sexToText);
                setTdText('tdBirth', 'stubs_birthday', dateToText);
                setTdText('tdCity', 'stubs_regHome');
                
                setTdText('tdRace', 'ra_raceName');
                setTdText('tdCertNum', 'stubs_idCard');
                setTdText('tdBusinessAdd', 'stuapl_fileBlgOrgAddress');
                
                setTdText('tdJobTitle', 'stuext_techTitle', techTilToText);
                setTdText('tdJobName', 'stuext_adminJob');
                
                setTdText('tdPolitical', 'stubs_pltAff', plaffToText);
                setTdText('tdStuSource', 'stuapl_sourceType', sSrcToText);
                setTdText('tdTel', 'stubs_phoneNumber');
                
                setTdText('tdAddress', 'stubs_contactAddress');
                setTdText('tdPostCode', 'stubs_postCode');
                
                setTdText('tdMajorArea', 'stuapl_applyMajor');
                setTdText('tdReserchField', 'ma_majorName');
                
                var unGrdtBean = xmlObj.find('stuedu_unGrdtSchoolBean'), bachelorBean = xmlObj.find('stuedu_bachelorSchoolBean'), postGrdtBean = xmlObj.find('stuedu_postGrdtSchoolBean'), modBean = xmlObj.find('stuedu_moDSchoolBean');
                //看是否有本科学历
                var isUnGrdt = xmlObj.find('stuedu_unGrdt').text(), unGrdtText = '';
                if (isUnGrdt == '1') {
                    var unGrdtMajor = xmlObj.find('stuedu_unGrdtMajor').text(), unGrdtNum = xmlObj.find('stuedu_unGrdtNumber').text(), unGrdtTime = dateToText(xmlObj.find('stuedu_unGrdtTime').text()), unGrdtSclName = unGrdtBean.find('unicd_unysyName').text();
                    unGrdtText = unGrdtTime + '  毕业于 ' + unGrdtSclName + ' 学校 ' + unGrdtMajor + ' 专业 证书编号: ' + unGrdtNum;
                }
                $('#tdUnGrdtData').text(unGrdtText);
                //看是否有学士学位
                var isBachelor = xmlObj.find('stuedu_bachelorDegree').text(), bachelorText = '';
                if (isBachelor == '1') {
                    var bachelorMajor = xmlObj.find('stuedu_bachelorMajor').text(), bachelorNum = xmlObj.find('stuedu_bachelorNumber').text(), bachelorTime = dateToText(xmlObj.find('stuedu_bachelorTime').text()), bachlorSclName = bachelorBean.find('unicd_unysyName').text();
                    bachelorText = bachelorTime + '  毕业于 ' + bachlorSclName + ' 学校 ' + bachelorMajor + ' 专业 证书编号: ' + bachelorNum;
                }
                $('#tdBachelorData').text(bachelorText);
                //看是否有研究生学历
                var isPostGrdt = xmlObj.find('stuedu_postGrdt').text(), postGrdtText = '';
                if (isPostGrdt != '0') {
                    postGrdtText = postGrdtText + isPostGrdt == '1' ? '硕士' : '博士';
                    var postGrdtMajor = xmlObj.find('stuedu_postGrdtMajor').text(), postGrdtNum = xmlObj.find('stuedu_postGrdtNumber').text(), postGrdtTime = dateToText(xmlObj.find('stuedu_postGrdtTime').text()), postGrdtSclName = postGrdtBean.find('unicd_unysyName').text();
                    postGrdtText = postGrdtText + unGrdtTime + '  毕业于 ' + postGrdtSclName + ' 学校 ' + postGrdtMajor + ' 专业 证书编号: ' + postGrdtNum;
                }
                $('#tdOtherEdu').text(postGrdtText);
                //看是否有研究生学位
                var isMod = xmlObj.find('stuedu_moDDegree').text(), modText = '';
                if (isMod != '0') {
                    modText = modText + isMod == '1' ? '硕士' : '博士';
                    var modMajor = xmlObj.find('stuedu_moDMajor').text(), modNum = xmlObj.find('stuedu_moDNumber').text(), modTime = dateToText(xmlObj.find('stuedu_moDTime').text()), modSclName = modBean.find('unicd_unysyName').text();
                    modText = modText + modTime + '  毕业于 ' + modSclName + ' 学校 ' + modMajor + ' 专业 证书编号: ' + modNum;
                }
                $('#tdOtherDegree').text(modText);
                //头像
                $('#tdAvatar img').attr('src', '../../userfiles/image/' + stuid + '.jpg');
            }
            else {
                //没查到学生信息
                $.curStudentId = '';
            }
            
        },
        beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
            var xmlObj = $(x.responseText);
            if (xmlObj.find('faultstring').text() == 'oops') {
                alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                return;
            }
            else {
                alert('error:' + x.responseText);
            }
        },
        complete: function(x){
        
        }
    });
}

//写入Flash的src参数
function flashVars(studentId){
    var servletUrl = window.location.protocol + '//' + window.location.host;
    return servletUrl + '/ZJUSCIX/common/camera.swf?nt=1&inajax=1&appid=1&input=' + studentId + '&ucapi=' + encodeURIComponent(servletUrl).toLowerCase() + '/ZJUSCIX/AvatarServlet';
}

//上传头像完毕回调函数
function updateavatar(sender, args){
    //alert('上传完毕');
    $('#tdAvatar img').attr('src', '../../userfiles/image/' + $.curStudentId + '.jpg');
}

$(function(){
    $.curStudentId = '';
});

/*
 *
 *
 */
$(window).load(function(){
    //通过报名号检索学生
    $('#searchButton').click(function(){
        var applyID = $.trim($('#inApplyNo').val());
        loadStuDetailData(applyID);
    });
    
    $('#inApplyNo').bind('keypress', function(evt){
        //搜索报名号只能输入数字
        //var code = (evt.keyCode ? evt.keyCode : evt.which);
        //return code >= 48 && code <= 57;
    });
    //单击头像区域
    $('#tdAvatar').click(function(){
        if ($.curStudentId != '') {
            $('#maskLayer').height($('#container').height()).show();
            $('#wndInner').html(AC_FL_RunContent('width', '540', 'height', '253', 'scale', 'exactfit', 'src', flashVars($.curStudentId), 'id', 'mycamera', 'name', 'mycamera', 'quality', 'high', 'bgcolor', '#ffffff', 'wmode', 'transparent', 'menu', 'false', 'swLiveConnect', 'true', 'allowScriptAccess', 'always'));
            $('#cameraWnd').show();
        }
        else {
            alert('当前没有可操作的学生');
        }
    }).find('img').bind('error', function(){
        $(this).attr('src', '../../userfiles/image/noavatar.gif');
    });
    //窗口关闭按钮
    $('#closeBtn').click(function(){
        $('#maskLayer').hide();
        $('#cameraWnd').hide();
    });
    //打印准考证
    $("#printBtn3").click(function(){
        var inApplyNo = $("#inApplyNo").val();
        
        var str = "../../DownloadServlet?method=get&inApplyNo=" + inApplyNo;
        $("#downframe").attr("src", str);
    });
});
